﻿package code {
	import flash.display.Shape;
	// represents a line derived from schwartz code
	public class Line extends Shape {
		protected var thickness:Number;  // thickness of the line
		public var a:Vertex;  // startpoint
		public var b:Vertex;  // endpoint
		protected var color:uint;  // color of the line
		public var startDist:Number;
		public var normal:Vertex;
		public var pointD:Number;
		public var go:GameObject; // to keep track of the gameobject that it is a part of
		public function Line(a:Vertex, b:Vertex, thickness:Number, color:uint) {
			this.a = a;
			this.b = b;
			this.thickness = thickness;
			this.color = color;
			if(a != null && b != null)
				init();
		}
		
		// generates the line
		public function init() {
			graphics.clear();
			graphics.lineStyle(thickness, 0x0000FF);
			graphics.moveTo(a.x, a.y);
			graphics.lineTo(b.x, b.y);
		}
		
		public function update(a:Vertex, b:Vertex):void {
			this.a = a;
			this.b = b;
			graphics.clear();
			init();
		}
		
		// gets theta in radians:
		public function getAngle():Number {
			var xLength:Number = b.x - a.x;
			var yLength:Number = b.y - a.y;
			return Math.atan2(yLength, xLength);
		}
		
		// distance:
		public function distance():Number {
			var xLength:Number = b.x - a.x;
			var yLength:Number = b.y - a.y;
			return Math.sqrt(xLength*xLength + yLength*yLength);
		}
		
		// gets normal vector
		public function getNormal():Vertex{
			var normalVertex:Vertex = new Vertex((b.y - a.y), -(b.x - a.x));
			var normalUnitVector:Vertex = Vertex.divide(Vertex.getMagnitude(normalVertex), normalVertex);
			normal = normalUnitVector;
			return normalUnitVector;
		}
		
		// gets point a on the plane
		public function getPoint():Number{
			pointD = Vertex.dot(normal, a);
			return pointD;
		}
		
	}
}